home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0017_Read and Write on TMemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  2.8 KB  |  140 lines

  1.  
  2. My small contribution to DELPHI programmers:
  3. Here follows a small unit that helps you use any derivative of TCustomEdit for 
  4. standard Pascal Input/Output.  Hope you find it useful.  It works the fine 
  5. with the TP yacc and lex for parsing the contents of a TMemo.  Could also 
  6. replace the need for WinCRT.
  7.  
  8. How to use it:
  9.  
  10. Uses
  11.    ...., EditText;
  12.  
  13. Var
  14.    F : Text;
  15.    Memo1, Memo2: TMemo;
  16.    S : String;
  17.    i : Integer;
  18.  
  19. Begin
  20.  
  21.   {Assumes the Memos have been created and exist on the form}
  22.    AssignDevice(System.Input, Memo1);
  23.    Reset(System.Input);
  24.    AssignDevice(System.Output, Memo2);
  25.    Rewrite(System.Output);
  26.  
  27.   {Now normal Reads and Writes work with Memo1 and Memo2.  ie.}
  28.   Writeln(S); Write(i:2);
  29.  
  30.  
  31.   {Also}
  32.    AssignDevice(F, Memo2);
  33.    Rewrite(F);
  34.    Writeln(F,S); Write(F,i:2);
  35.  
  36. end;
  37.  
  38.  
  39. Source:
  40.  
  41. unit EditText;
  42. {
  43.  
  44.       Written by Kiriakos Vlahos (kvlahos.@lbs.lon.ac.uk)
  45.       Freeware   -  Please send comments of improvements.
  46.    
  47. }
  48.  
  49. interface
  50.  
  51. uses
  52.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  53.   StdCtrls, Forms, Dialogs;
  54.  
  55.   procedure AssignDevice(var T: Text; NewEditComponent: TCustomEdit);
  56.  
  57. implementation
  58.  
  59. type
  60.   EditData = record
  61.     Edit: TCustomEdit;
  62.     Filler: Array [1..12] of Char;
  63.   end;
  64.  
  65. function EditWrite(var F: TTextRec): Integer; far;
  66. begin
  67.   with F do
  68.   begin
  69.     BufPtr^[BufPos] := #0;
  70.     EditData(F.UserData).Edit.SetSelTextBuf(PChar(BufPtr));
  71.     BufPos := 0;
  72.   end;
  73.   EditWrite := 0;
  74. end;
  75.  
  76. function EditRead(var F: TTextRec): Integer; far;
  77. Var
  78.   CurPos : Integer;
  79. begin
  80.   with F do
  81.     with EditData(UserData) do begin
  82.       BufPos := 0;
  83.       Edit.SelLength := BufSize;
  84.       Edit.GetSelTextBuf(PChar(BufPtr), BufSize);
  85.       BufEnd := StrLen(PChar(BufPtr));
  86.       Edit.SelStart := Edit.SelStart + BufEnd;
  87.     end;
  88.   EditRead := 0;
  89. end;
  90.  
  91. function EditFlush(var F: TTextRec): Integer; far;
  92. begin
  93.   F.BufPos := 0;
  94.   F.BufEnd := 0;
  95.   EditFlush := 0;
  96. end;
  97.  
  98. function EditOpen(var F: TTextRec): Integer; far;
  99. begin
  100.   with F do
  101.   begin
  102.     if Mode = fmInput then
  103.     begin
  104.       InOutFunc := @EditRead;
  105.       FlushFunc := nil;
  106.       EditData(F.UserData).Edit.SelStart := 0;
  107.     end
  108.     else
  109.     begin
  110.       Mode := fmOutput;
  111.       InOutFunc := @EditWrite;
  112.       FlushFunc := @EditWrite;
  113.     end;
  114.     EditOpen := 0;
  115.   end;
  116. end;
  117.  
  118. function EditIgnore(var F: TTextRec): Integer; far;
  119. begin
  120.   EditIgnore := 0;
  121. end;
  122.  
  123. procedure AssignDevice(var T: Text; NewEditComponent: TCustomEdit);
  124. begin
  125.   with TTextRec(T) do
  126.   begin
  127.     Handle := $FFFF;
  128.     Mode := fmClosed;
  129.     BufSize := SizeOf(Buffer)-1;
  130.     BufPtr := @Buffer;
  131.     OpenFunc := @EditOpen;
  132.     CloseFunc := @EditIgnore;
  133.     Name[0] := #0;
  134.     EditData(UserData).Edit:= NewEditComponent;
  135.   end;
  136. end;
  137.  
  138. end.
  139.  
  140.